<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Crawlergroup extends MX_Controller {
	
	function __construct() {
		$this->load->model ( 'category_model' );
		parent::__construct ();
	}
	
	function index() {
		?>
		<table width="100%" border="">
		<thead>
			<tr>
				<th>Name</th>
				<th>Description</th>
				<th>Crawler Url</th>
			</tr>
		</thead>
		<tbody>
		<?php
		$this->category_model->query ( 'TRUNCATE TABLE catalog_category' );
		$links = $this->_extract_category_links ( 'http://truyen.vnsharing.net/DanhSach' );
		foreach ($links as $link) {
			$data = $this->_extract_category_infor($link);
			if (strlen($data['name']) > 0) {
				//$model = new category_model();
				//$model->setEntireData($data);
				//$model->save();
			}
		}
		?>
		</tbody>
		</table>
		<?php 
		echo('<span>Total Extract: </span><h4>'.count($links).'</h4> links');
	}
	
	function _extract_category_links($url) {
		$data = file_get_contents ( $url );
		$data = str_replace ( '<a href="/', '<a href="http://truyen.vnsharing.net/', $data );
		if (preg_match_all ( '/((?:http|https):\/\/(?:www\.)*(?:[a-zA-Z0-9_\-]{1,15}\.+[a-zA-Z0-9_]{1,}){1,}(?:[a-zA-Z0-9_\/\.\-\?\&\:\%\,\!\;]*))/', $data, $links )) {
			$filter_level0 = array ();
			foreach ( $links ['0'] as $link ) {
				if (stripos ( $link, '/TheLoai/' ) > 0) {
					$filter_level0[addslashes($link)] = $link;					
				}
			}
			return $filter_level0;
		}
		return array ();
	}
	
	function _extract_category_infor($link) {
		$data['entity_type_id'] = '1';
		$data['name'] = trim(str_replace('-', ' ', substr($link, strpos($link, '/TheLoai/') + strlen('/TheLoai/'), strlen($link))));
		$data['description'] = strtolower(str_replace(' ', '-', $data['name']));
		?>
		<tr>
			<td valign="top"><?php echo $data['name'] ?></td>
			<td valign="top"><?php echo $data['description'] ?></td>
			<td valign="top"><a href="<?php echo $link ?>" target="_blank"><?php echo $link ?></a></td>
		</tr>
		<?php
		return $data;
	}
}
?>
